package com.kaishengit.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.Goal;
import com.kaishengit.service.GoalService;

public class NewGoalAdd extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");
		
		String goalname = request.getParameter("goalname");
		String goaldesc = request.getParameter("goaldesc");
		
		String manager = request.getParameter("manager");
		
		String project = request.getParameter("project");
		System.out.println(project);
		
		GoalService goalService = new GoalService();
		
		List<Goal> goalOlds = goalService.findGoalnamesByProjectname(project);
		
		boolean flag = true;
		
		for(Goal goal : goalOlds) {
			if(goal.getName().equals(goalname)) {
				flag = false;
				response.sendRedirect("newgoalview.jspx?state=10001");	
			}
		}
		
		if(flag) {
			Goal goal = new Goal();
			
			UUID uuid = UUID.randomUUID();
			
			goal.setId(uuid.toString());
			goal.setName(goalname);
			goal.setDesc(goaldesc);
			goal.setCreatetime(DateUtil.getNow());
			goal.setEmployeeid(manager);
			goal.setProjectid(project);
			
			
			
			goalService.save(goal);
			
			response.sendRedirect("goallistview.jspx?project=" + project);
		}	
		
			
	}

}
